import Vue from 'vue'

/*
 * 常用方法
 */
import prototype_api from './nddyny/prototype/api.js'
import prototype_app from './nddyny/prototype/app.js'
import prototype_dict from './nddyny/prototype/dict.js'
import prototype_map from './nddyny/prototype/map.js'
import prototype_R from './nddyny/prototype/R.js'
import prototype_ResultCode from './nddyny/prototype/ResultCode.js'
import prototype_nddyny from './nddyny/prototype/nddyny.js'
import prototype_user from './nddyny/prototype/user.js'
Vue.prototype.$api = prototype_api;
Vue.prototype.$app = prototype_app;
Vue.prototype.$dict = prototype_dict;
Vue.prototype.$map = prototype_map;
Vue.prototype.$R = prototype_R;
Object.keys(prototype_ResultCode).forEach(key => {
    prototype_ResultCode[key] = prototype_app.info.APP_NAME + prototype_ResultCode[key];
});
Vue.prototype.$ResultCode = prototype_ResultCode;
Vue.prototype.$nddyny = prototype_nddyny;
Vue.prototype.$user = prototype_user;

/*
 * filter
 */
Vue.filter('floor', (value, scale, is_parseFloat) => {
    return prototype_nddyny.number.floor(value, scale, is_parseFloat);
})
Vue.filter('isNaN', (value) => {
    return isNaN(value);
})
Vue.filter('dateFormat', (datetime, format) => {
    return prototype_nddyny.date.format(datetime, format);
})
Vue.filter('timeFormat', (time, format) => {
    return prototype_nddyny.time.format(time, format);
})
Vue.filter('stringInArray', (string, key) => {
    return prototype_nddyny.string.array.inArray(string, key);
})
Vue.filter('isArray', (array) => {
    return prototype_nddyny.types.isArray(array);
})
Vue.filter('bc', (number1, action, number2) => {
    return prototype_nddyny.bc[action](number1, number2);
})
Vue.filter('bc3', (number1, action, number2, action3, number3, floor3) => {
    let value3 = prototype_nddyny.bc[action3](number2, number3);
    if (floor3) {
        value3 = prototype_nddyny.number.floor(value3, floor3);
    }
    return prototype_nddyny.bc[action](prototype_nddyny.number.floor(number1, floor3), value3, floor3);
})
Vue.filter('inArray', (array, key) => {
    return array.includes(key);
})
Vue.filter('ossUrl', (uri, width, height) => {
    if (prototype_nddyny.string.isUrlHavePrefix(uri)) {
        return uri;
    }
    let result = prototype_app.oss_base_url + uri;
    if (width || height) {
        result += '?x-oss-process=image/resize,m_lfit,limit_0';
        if (width) {
            result += ',w_' + width;
        }
        if (height) {
            result += ',h_' + height
        }
    }
    return result;
})
Vue.filter('dict', (value, key) => {
    return prototype_dict.get(key, value);
})
Vue.filter('substr', (string, a, b) => {
    return string.substr(a, b);
})
Vue.filter('m2km', (distance) => {
    if (distance >= 1000) {
        return prototype_nddyny.number.floor(prototype_nddyny.bc.div(distance, 1000), 1) + 'km';
    } else {
        return prototype_nddyny.number.floor(distance, 0) + 'm';
    }
})



/*
 * config
 */
Vue.config.productionTip = prototype_app.env == 'production' ? false : true


/*
 * window
 */
// #ifdef H5
window.nlog = function(content, color = undefined, autoC = true) {
    if (typeof(content) === 'object') {
        content = prototype_nddyny.json.format.string(content);
    }
    switch (color) {
        case 'red':
            color = 'a82064';
            break;
        case 'blue':
            color = '02468a';
            break;
        default:
            color = 'ec7600';
    }
    if (autoC === true) {
        content = '%c' + content;
    }
    console.log(content, 'color: #' + color);
}
// #endif
